package org.daochong.lang;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.daochong.lang.JdbcTemplate;
import org.daochong.lang.JdbcTemplateFactory;
import org.daochong.lang.JsonAccessException;
import org.daochong.lang.Properties;
import org.daochong.lang.uai.UniteAccessInterfaceFactory;
import org.daochong.lang.ucm.ConfigurationContainer;

public class DefaultJdbcTemplateProvider implements JdbcTemplateProvider {

	private ConfigurationContainer configurationContainer;
	private UniteAccessInterfaceFactory uaiFactory;
	private JdbcTemplateFactory factory;
	private JdbcTemplate jdbcTemplate;
	private DataSource dataSource;
	private String configId;
	private String groupId;
	private String mappingConfigId;
	private String mappingGroupId;
	private String propConfigId;
	private String propGroupId;

	public String getPropConfigId() {
		return propConfigId;
	}

	public void setPropConfigId(String propConfigId) {
		this.propConfigId = propConfigId;
	}

	public String getPropGroupId() {
		return propGroupId;
	}

	public void setPropGroupId(String propGroupId) {
		this.propGroupId = propGroupId;
	}

	public ConfigurationContainer getConfigurationContainer() {
		return configurationContainer;
	}

	public void setConfigurationContainer(ConfigurationContainer configurationContainer) {
		this.configurationContainer = configurationContainer;
	}

	public UniteAccessInterfaceFactory getUaiFactory() {
		return uaiFactory;
	}

	public void setUaiFactory(UniteAccessInterfaceFactory uaiFactory) {
		this.uaiFactory = uaiFactory;
	}

	public JdbcTemplateFactory getFactory() {
		return factory;
	}

	public void setFactory(JdbcTemplateFactory factory) {
		this.factory = factory;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public String getConfigId() {
		return configId;
	}

	public void setConfigId(String configId) {
		this.configId = configId;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getMappingConfigId() {
		return mappingConfigId;
	}

	public void setMappingConfigId(String mappingConfigId) {
		this.mappingConfigId = mappingConfigId;
	}

	public String getMappingGroupId() {
		return mappingGroupId;
	}

	public void setMappingGroupId(String mappingGroupId) {
		this.mappingGroupId = mappingGroupId;
	}

	protected ConfigurationContainer requestConfigurationContainer() {
		return null;
	}

	protected Properties requestProperties() {
		Properties p = null;
		if (this.getPropConfigId() != null) {
			if (this.getPropGroupId() != null) {
				p = this.requestConfigurationContainer().getConfiguration(getPropConfigId(), getPropGroupId())
						.getProperties();
			} else {
				p = this.requestConfigurationContainer().getConfiguration(getPropConfigId()).getProperties();
			}
		}
		return p;
	}

	public JdbcTemplate requestJdbcTempalte() throws JsonAccessException {
		JdbcTemplate jdbc = null;
		if (this.jdbcTemplate != null) {
			return this.jdbcTemplate;
		} else if (this.uaiFactory != null) {
			if (this.getConfigId() != null && this.getGroupId() != null) {
				jdbc = this.uaiFactory.getTemplate(getConfigId(), getGroupId(), JdbcTemplate.class);
			} else if (this.getConfigId() != null) {
				jdbc = this.uaiFactory.getTemplate(getConfigId(), JdbcTemplate.class);
			} else {
				jdbc = this.uaiFactory.getTemplate(JdbcTemplate.class);
			}
		} else if (this.factory != null) {
			jdbc = this.factory.getJdbcTemplate();
		} else if (this.dataSource != null) {
			jdbc = new JdbcTemplate(this.dataSource);
		}
		Properties p = requestProperties();
		if (p != null) {
			Map<String, Object> params = new HashMap<String, Object>();
			params.putAll(p.toMap());
			BeanUtil.pushBean(params, jdbc);
		}
		return jdbc;
	}

	public JdbcTemplate requestJdbcTempalte(String configId) throws JsonAccessException {
		if (this.getUaiFactory() != null) {
			return this.getUaiFactory().getTemplate(configId, JdbcTemplate.class);
		}
		return null;
	}

	public JdbcTemplate requestJdbcTempalte(String configId, String groupId) throws JsonAccessException {
		if (this.getUaiFactory() != null) {
			return this.getUaiFactory().getTemplate(configId, groupId, JdbcTemplate.class);
		}
		return null;
	}

}
